/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "ui图像视频.h"

#include "../ui绘制模板.h"
#include "core/属性.h"


static uint16 g视频框属性ID = 0;
static uint16 g视频框变换ID = 0;
static uint16 g视频框颜色ID = 0;
static uint16 g视频框元素ID = 0;
//static uint16 g视频框属性ID = 0;

static uint16 g捕捉点变换ID = 0;
static uint16 g捕捉点颜色ID = 0;
static uint16 g捕捉点元素ID = 0;


DEF_单色绘制ID(选框);


static void f_draw视频框(C_Widget* self, S_2D画布* 画布) {
	C_视频框* view = dynamic_cast<C_视频框*>(self);

	static S_纹理* 占位纹理 = f_tex_创建图像纹理(view->m_UIctx->m_Ctx, {16,16,1}, E_纹理格式::e_tf_RGBA8U_U, E_纹理维度类型::e_2D);
	view->m_帧[0] = 占位纹理;

	g视频框属性ID = f_vg_gen属性(画布);
	//g视频框变换ID = f_vg_gen变换(画布);
	//g视频框颜色ID = f_vg_gen颜色(画布);
	view->m_笔刷 = f_vg_gen图像笔刷(画布, g视频框属性ID, g视频框变换ID, 占位纹理);
	
	g视频框元素ID = f_vg_画纹理面(画布, view->m_笔刷);
	f_vg_set实例数量(画布, g视频框元素ID, 1);
	
	f_vg_set单色(画布, g视频框颜色ID, 0, { 10,10,10,255 });




	//======================== 捕捉点绘制 ===============================
	{
		//g捕捉点变换ID = f_vg_gen变换(画布);
		//g捕捉点颜色ID = f_vg_gen颜色(画布);
		//auto pen = f_vg_gen单色描边(画布, g捕捉点变换ID, g捕捉点颜色ID, g视频框属性ID);

		//g捕捉点元素ID = f_vg_绘制矩形(画布, pen, false);

	}
	
	//======================== 选框绘制 ============================
	{
		//f_ui绘制模板_选框绘制(画布, gid_选框绘制变换, gid_选框绘制颜色, gid_选框绘制元素, g视频框属性ID);
	}



	if (view->m_帧.size()) {
		//f_vg_set纹理(view->m_笔刷, 0, view->m_帧[0]);
		//S_绘图设备配置* pip = (S_绘图设备配置*)(view->m_帧[0]->m_Ctx->m_绘图设备);
		std::vector<uint8> data(16 * 16 * 4, 100);
		f_tex_填充纹理数据(view->m_帧[0], data.data(), sizeof(S_RGBA8UI), { 16,16,1 });

		f_tex_安装纹理采样器(view->m_帧[0], view->m_UIctx->m_Mat->m_采样器);
	}

}


static void f_set视频框部件变换(C_Widget* self) {
	C_视频框* view = dynamic_cast<C_视频框*>(self);

	S_2D顶点属性 属性 = {};
	//属性.m_偏移 = self->m_Pos;
	
	属性.m_偏移 = self->m_父部件->m_扩展视口属性->m_中心;
	属性.m_比例 = self->m_Scale;
	f_vg_set属性(self->m_画布, g视频框属性ID, 属性);


	mat3X2 变换 = {};
	变换.row0 = {};
	//变换.row1 = self->m_Size;
	变换.row1 = self->f_getOriginalSize();
	变换.row2 = {1,0};
	f_vg_set变换(self->m_画布, g视频框变换ID, 0, 变换);

	//std::cout << "self->m_扩展视口属性->m_比例A = " << self << " + " << view->m_Size.x << std::endl;
	if (self->m_父部件->m_扩展视口属性) {
		S_2D顶点属性 属性 = { {1.0f, 1.0f} };
		//属性.m_比例 = self->m_父部件->m_扩展视口属性->m_比例;
		//属性.m_偏移 = self->m_父部件->m_扩展视口属性->m_中心;
		//f_vg_set属性(self->m_画布, g视频框属性ID, 属性);
	}

}


static void f_播放视频框动画(C_Widget* self) {
	C_视频框* view = dynamic_cast<C_视频框*>(self);

	if (view->m_槽纹理更新) {
		f_vg_set纹理(view->m_笔刷, 0, view->m_帧[0]);
		f_tex_安装纹理采样器(view->m_帧[0], view->m_UIctx->m_Mat->m_采样器);

		view->m_槽纹理更新 = false;
	}
	
}



C_视频框::C_视频框(S_UI创建环境& ctx, uint8 缓存数量) :C_Widget(&ctx) {
	mf_变换修改 = f_set视频框部件变换;
	mf_DrawFun = f_draw视频框;
	mf_动画更新 = f_播放视频框动画;

	m_笔刷 = {};
	m_帧.resize(缓存数量);
	for (uint32 i = 0; i < 缓存数量; ++i) {
		m_帧[i] = 0;
	}

	m_槽纹理更新 = false;
}

C_视频框::~C_视频框() {
}

void C_视频框::f_填充像素(uint8* pix, uvec3 dim, uint8 color_bit) {
	f_tex_填充纹理数据(m_帧[0], pix, sizeof(S_RGB8UI), { 16,16,1 });
}

void C_视频框::f_set槽纹理(S_纹理* tex, uint8 offset) {
	m_帧[offset] = tex;
	m_槽纹理更新 = true;
	m_GPU参数重置 = true;

	f_tex_安装纹理采样器(tex, m_UIctx->m_Mat->m_采样器);
}



void f_视频框_填充视频捕捉点(C_视频框* box, const std::vector<S_Rect2Df>& 点坐标, const std::vector<S_RGBA8UI>& 点颜色) {
	uint32 num = 点坐标.size();
	if(!num) return;


	f_vg_resize参数实例(box->m_画布, g捕捉点变换ID, num);
	f_vg_resize参数实例(box->m_画布, g捕捉点颜色ID, num);



	mat3X2* 变换 = f_vg_map绘制数据指针<mat3X2>(box->m_画布, g捕捉点变换ID);
	uint32* 颜色 = f_vg_map绘制数据指针<uint32>(box->m_画布, g捕捉点颜色ID);

	const S_Rect2Df* rect = 点坐标.data();
	const S_RGBA8UI* color = 点颜色.data();
	

	for (uint32 i = 0; i < num; ++i) {
		变换[i].row2 = {1,0};
		变换[i].row1 = rect[i].extent;
		变换[i].row0 = rect[i].offset - rect[i].extent*0.5;

		颜色[i] = f_PackData4X8(color[i]);
	}

	f_vg_unmap绘制数据指针(box->m_画布, g捕捉点颜色ID);
	f_vg_unmap绘制数据指针(box->m_画布, g捕捉点变换ID);

	
	f_vg_set实例数量(box->m_画布, g捕捉点元素ID, num);

	box->m_GPU参数重置 = true;
}

void f_视频框_设置选取(C_视频框* box, const S_Rect2Df& 点坐标, const S_RGBA8UI& 点颜色) {
	mat3X2 变换;
	变换.row0 = 点坐标.offset;
	变换.row1 = 点坐标.extent;
	变换.row2 = {1, 0};

	f_vg_set变换(box->m_画布, gid_选框绘制变换, 0, 变换);
	f_vg_set单色(box->m_画布, gid_选框绘制颜色, 0, 点颜色);

	f_vg_set实例数量(box->m_画布, gid_选框绘制元素, 1);
}

void f_视频框_关闭选取(C_视频框* box) {
	f_vg_set实例数量(box->m_画布, gid_选框绘制元素, 0);
}






static void f_图像框视口布局(C_Widget* self, C_Widget** w, uint32 num) {
	C_图像框* box = dynamic_cast<C_图像框*>(self);

	
}

static void f_draw图像框(C_Widget* self, S_2D画布* 画布) {
	C_图像框* box = dynamic_cast<C_图像框*>(self);

	uint32 属性 = f_vg_gen属性(画布);
	//S_凸边图元 图元{};
	//图元.属性 = f_vg_get属性(画布, box->m_背景绘制.m_属性);
	box->m_背景绘制.f_Draw(画布, 属性, box->m_纹理绘制类型);

}

static void f_图像框部件变换(C_Widget* self) {
	C_图像框* box = dynamic_cast<C_图像框*>(self);

	mat3X2 mat;
	mat.row0 = {};
	mat.row1 = self->m_Size;
	mat.row2 = {6,0};

	box->m_背景绘制.f_alloc(1);
	box->m_背景绘制.f_set(0, mat, S_Widget预设图元::m_圆角矩形, {}, *f_prop_TexProp(box->m_纹理属性));
	
}


C_图像框::C_图像框(S_UI创建环境& ctx) :C_Widget(&ctx) {
	mf_布局Fun = f_图像框视口布局;
	mf_DrawFun = f_draw图像框;
	mf_变换修改 = f_图像框部件变换;

}

C_图像框::~C_图像框() {
}



C_Widget* f_ui_创建图像框(S_UI创建环境& ctx) {
	C_图像框* box = new C_图像框(ctx);
	box->m_纹理绘制类型 = E_笔刷类型::e_纹理;
	return box;
}

C_Widget* f_ui_创建过程纹理图像框(S_UI创建环境& ctx) {
	C_图像框* box = new C_图像框(ctx);
	box->m_纹理绘制类型 = E_笔刷类型::e_噪波;
	return box;
}

void f_ui_bind图像框纹理属性(C_Widget* self, S_Props& prop) {
	C_图像框* box = static_cast<C_图像框*>(self);
	box->m_纹理属性 = prop;
}